package com.smart.support.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.smart.common.entities.ajax.AjaxResult;
import com.smart.common.entities.constant.SmartConstant;
import com.smart.common.entities.option.SelectOption;
import com.smart.common.entities.option.TreeOption;
import com.smart.manager.system.repository.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Administrator
 */
@Api(tags = "系统管理公共资源")
@RestController
@RequestMapping(SmartConstant.MANAGER_API_PREFIX + "system/support")
@RequiredArgsConstructor
public class SystemApi {


    private final ModuleRepository moduleRepository;
    private final DictRepository dictRepository;
    private final ApiRepository apiRepository;
    private final MenuRepository menuRepository;
    private final DeptRepository deptRepository;
    private final RoleRepository roleRepository;
    private final PositionRepository positionRepository;
    private final OrganRepository organRepository;
    private final ScheduleAppRepository scheduleAppRepository ;
    private final AccountRepository accountRepository ;

    @GetMapping("/dict/{code}")
    @ApiOperation(value = "数据字典")
    public AjaxResult<List<SelectOption>> readSystemDict(@PathVariable String code) {
        List<SelectOption> dictList = dictRepository.readDict(code);
        return AjaxResult.data(dictList);
    }


    @ApiOperation(value = "模块字典")
    @GetMapping("moduleOptions")
    public AjaxResult<List<SelectOption>> moduleOptions() {
        List<SelectOption> result = moduleRepository.getOptions();
        return AjaxResult.data(result);
    }

    @GetMapping("apiOptions")
    @ApiOperation(value = "API数据字典", notes = "包含权限表达式")
    public AjaxResult<List<SelectOption>> apiOptions() {
        List<SelectOption> dictList = apiRepository.getOptions(true);
        return AjaxResult.data(dictList);
    }


    @ApiOperation(value = "菜单字典")
    @GetMapping("menuTreeOptions")
    public AjaxResult<List<TreeOption>> menuTreeOptions() {
        List<TreeOption> result = menuRepository.getTreeOptions();
        return AjaxResult.data(result);
    }


    @ApiOperation(value = "职务字典")
    @GetMapping("positionOptions")
    public AjaxResult<List<SelectOption>> positionOptions() {
        List<SelectOption> result = positionRepository.getOptions();
        return AjaxResult.data(result);
    }


    @ApiOperation(value = "部门字典")
    @GetMapping("deptTreeOptions/{organCode}")
    public AjaxResult<List<TreeOption>> deptTreeOptions(@PathVariable String organCode) {
        List<TreeOption> result = deptRepository.getTreeOptions(organCode);
        return AjaxResult.data(result);
    }

    @ApiOperation(value = "操作员字典")
    @GetMapping("operatorOptions")
    public AjaxResult<List<SelectOption>> operatorOptions() {
        List<SelectOption> result = accountRepository.getOptions();
        return AjaxResult.data(result);
    }
    @ApiOperation(value = "客服字典")
    @GetMapping("customerServiceOptions")
    public AjaxResult<List<SelectOption>> CustomerServiceOptions() {
        List<SelectOption> result = accountRepository.getCustomerServiceOptions();
        return AjaxResult.data(result);
    }


    @ApiOperation(value = "机构字典树")
    @GetMapping("organTreeOptions")
    public AjaxResult<List<TreeOption>> organTreeOptions() {
        List<TreeOption> result = organRepository.getTreeOptions();
        return AjaxResult.data(result);
    }










    @ApiOperation(value = "菜单按钮关联字典")
    @GetMapping("menuAndApiOptions")
    public AjaxResult<List<JSONObject>> menuAndApiOptions() {
        List<JSONObject> result = menuRepository.menuAndApiOptions();
        return AjaxResult.data(result);
    }


    @ApiOperation(value = "角色字典")
    @GetMapping("roleOptions")
    public AjaxResult<List<SelectOption>> roleOptions(String accountType) {
        String type = StrUtil.isBlank(accountType) ? StrUtil.EMPTY : accountType;
        List<SelectOption> result = roleRepository.getOptions(type);
        return AjaxResult.data(result);
    }


    @ApiOperation(value = "定时程序字典")
    @GetMapping("scheduleAppOptions")
    public AjaxResult<List<SelectOption>> scheduleApp() {
        List<SelectOption> result = scheduleAppRepository.getOptions();
        return AjaxResult.data(result);
    }




}
